<template>
  <div v-show="support" ref="drop" class="uploader-drop" :class="dropClass">
    <slot></slot>
  </div>
</template>

<script>
  import { uploaderMixin, supportMixin } from '../common/mixins'

  export default {
    name: 'ChunkFileUploaderDrop',
    mixins: [uploaderMixin, supportMixin],
    data() {
      return {
        dropClass: ''
      }
    },
    async mounted() {
      await this.$nextTick()
      const dropEle = this.$refs.drop
      const uploader = this.uploader.uploader
      uploader.assignDrop(dropEle)
      uploader.on('dragenter', this.onDragEnter)
      uploader.on('dragleave', this.onDragLeave)
      uploader.on('drop', this.onDrop)
    },
    beforeDestroy() {
      const dropEle = this.$refs.drop
      const uploader = this.uploader.uploader
      uploader.off('dragenter', this.onDragEnter)
      uploader.off('dragleave', this.onDragLeave)
      uploader.off('drop', this.onDrop)
      uploader.unAssignDrop(dropEle)
    },
    methods: {
      onDragEnter() {
        this.dropClass = 'uploader-dragover'
      },
      onDragLeave() {
        this.dropClass = ''
      },
      onDrop() {
        this.dropClass = 'uploader-droped'
      }
    }
  }
</script>

<style>
  .uploader-drop {
    position: relative;
    padding: 10px;
    overflow: hidden;
    background-color: #f5f5f5;
    border: 1px dashed #ccc;
  }

  .uploader-dragover {
    background-color: #f7f7f7;
    border-color: #999;
  }
</style>
